package dk.pun.aot.client.view;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import dk.pun.aot.client.widget.CharacterIdRadioButton;
import dk.pun.aot.client.widget.CharacterRenameDialog;
import dk.pun.aot.client.widget.ConfirmDialog;
import dk.pun.aot.shared.character.CharacterId;

public class SelectCharacterViewImpl extends Composite implements SelectCharacterView {

	private static SelectCharacterViewUiBinder uiBinder = GWT.create(SelectCharacterViewUiBinder.class);

	@UiTemplate("SelectCharacterView.ui.xml")
	interface SelectCharacterViewUiBinder extends UiBinder<Widget, SelectCharacterViewImpl> {}

	@UiField VerticalPanel charactersPanel;
	@UiField Button selectButton;
	@UiField Button renameButton;
	@UiField Button deleteButton;
	@UiField TextBox newCharacterName;
	@UiField Button newCharacterButton;
	
	private Presenter presenter;
	private List<CharacterIdRadioButton> radioButtons;
	
	public SelectCharacterViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setCharacters(Iterator<CharacterId> characterIdIterator) {
		this.charactersPanel.clear();
		this.radioButtons = new ArrayList<CharacterIdRadioButton>();
		while (characterIdIterator.hasNext()) {
			CharacterIdRadioButton radioButton = new CharacterIdRadioButton(characterIdIterator.next());
			this.radioButtons.add(radioButton);
			this.charactersPanel.add(radioButton);
		}
	}
	
	@Override
	public void setSelectedCharacter(CharacterId characterId) {
		if (characterId != null) {
			for (CharacterIdRadioButton radioButton : this.radioButtons) {
				if (radioButton.getCharacterId().equals(characterId)) {
					radioButton.setValue(true);
					break;
				}
			}
		} else {
			for (CharacterIdRadioButton radioButton : this.radioButtons) {
					radioButton.setValue(false);
			}
		}
	}

	@Override
	public void clearCharacterSelection() {
		if (this.radioButtons != null) {
			for (CharacterIdRadioButton radioButton : radioButtons) {
				radioButton.setValue(false);
			}
		}
	}

	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@UiHandler("selectButton")
	public void selectButtonClicked(ClickEvent event) {
		CharacterId characterId = this.getSelectedRadioButton();
		if (characterId != null) {
			this.presenter.onCharacterSelected(characterId);
		}
	}
	
	@UiHandler("renameButton")
	public void renameButtonClicked(ClickEvent event) {
		CharacterRenameDialog characterRenameDialog = new CharacterRenameDialog(this.presenter, this.getSelectedRadioButton());
		characterRenameDialog.show();
		characterRenameDialog.center();
	}
	
	@UiHandler("deleteButton")
	public void deleteButtonClicked(ClickEvent event) {
		final CharacterId characterId = this.getSelectedRadioButton();
		if (characterId != null) {
			String message = "Are you sure you want to delete character '" + characterId.getName() + "'?";
			ConfirmDialog confirmDialog = new ConfirmDialog(message, new Command() {
				
				@Override
				public void execute() {
					presenter.onCharacterDeleted(characterId);
				}
			});
			confirmDialog.show();
			confirmDialog.center();
		}
	}
	
	@UiHandler("newCharacterButton")
	public void characterButtonClicked(ClickEvent event) {
		String newName = this.newCharacterName.getText();
		this.newCharacterName.setText("");
		if (this.presenter != null && !newName.isEmpty()) {
			this.presenter.onCharacterAdded(newName);
		}
	}
	
	@UiHandler("newCharacterName")
	public void characterNameKeyPressed(KeyPressEvent event) {
		if (KeyCodes.KEY_ENTER == event.getNativeEvent().getKeyCode()) {
			String newName = this.newCharacterName.getText();
			this.newCharacterName.setText("");
			if (this.presenter != null && !newName.isEmpty()) {
				this.presenter.onCharacterAdded(newName);
			}
		}
	}
	
	@Override
	public Widget asWidget() {
		return this;
	}
	
	private CharacterId getSelectedRadioButton() {
		CharacterId characterId = null;
		for (CharacterIdRadioButton radioButton : this.radioButtons) {
			if (radioButton.getValue()) {
				characterId = radioButton.getCharacterId();
				break;
			}
		}
		return characterId;
	}
}
